package pe.edu.upc.e41a.javasoft.domain;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;
import pe.edu.upc.e41a.javasoft.service.AvisoService;

@Configurable
@Component
@RooDataOnDemand(entity = Aviso.class)
public class AvisoDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<Aviso> data;

	@Autowired
    private UsuarioDataOnDemand usuarioDataOnDemand;

	@Autowired
    AvisoService avisoService;

	public Aviso getNewTransientAviso(int index) {
        Aviso obj = new Aviso();
        setFechaFin(obj, index);
        setFechaInicio(obj, index);
        setMensaje(obj, index);
        setPosicion(obj, index);
        setTitulo(obj, index);
        setUsuario(obj, index);
        return obj;
    }

	public void setFechaFin(Aviso obj, int index) {
        Date fechaFin = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setFechaFin(fechaFin);
    }

	public void setFechaInicio(Aviso obj, int index) {
        Date fechaInicio = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setFechaInicio(fechaInicio);
    }

	public void setMensaje(Aviso obj, int index) {
        String mensaje = "mensaje_" + index;
        if (mensaje.length() > 100) {
            mensaje = mensaje.substring(0, 100);
        }
        obj.setMensaje(mensaje);
    }

	public void setPosicion(Aviso obj, int index) {
        Integer posicion = new Integer(index);
        obj.setPosicion(posicion);
    }

	public void setTitulo(Aviso obj, int index) {
        String titulo = "titulo_" + index;
        if (titulo.length() > 20) {
            titulo = titulo.substring(0, 20);
        }
        obj.setTitulo(titulo);
    }

	public void setUsuario(Aviso obj, int index) {
        Usuario usuario = usuarioDataOnDemand.getRandomUsuario();
        obj.setUsuario(usuario);
    }

	public Aviso getSpecificAviso(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        Aviso obj = data.get(index);
        Integer id = obj.getCodigo();
        return avisoService.findAviso(id);
    }

	public Aviso getRandomAviso() {
        init();
        Aviso obj = data.get(rnd.nextInt(data.size()));
        Integer id = obj.getCodigo();
        return avisoService.findAviso(id);
    }

	public boolean modifyAviso(Aviso obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = avisoService.findAvisoEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'Aviso' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<Aviso>();
        for (int i = 0; i < 10; i++) {
            Aviso obj = getNewTransientAviso(i);
            try {
                avisoService.saveAviso(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
